#include "musiclyric.h"
#include "ui_musiclyric.h"
#include "musicplayer.h"
MusicLyric::MusicLyric(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicLyric)
{
    ui->setupUi(this);

//    pModelForm4 = new QStandardItemModel(ui->tableViewLy);
//    ui->tableViewLy->setModel(pModelForm4);
//    pModelForm4->setHorizontalHeaderLabels(QStringList() << "歌词");

        // 获取歌词
    //创建一个网络访问管理对象
    managerLy = new QNetworkAccessManager(this);
    connect(managerLy, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinishedLy(QNetworkReply*)));

    playlistLy = new QMediaPlaylist;
    playlistLy->setCurrentIndex(1);

    playerLy = new QMediaPlayer;
    playerLy->setPlaylist(playlistLy);
    connect(playerLy,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));

    playerLy->play();


    }

void MusicLyric::replyFinishedLy(QNetworkReply* reply)
{
    qDebug()<<"--------\"hello\"--------"<<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)<<endl;
//    QNetworkRequest::HttpStatusCodeAttribute网址返回的状态码
    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200)
    {
        qDebug()<<"访问服务器成功"<<endl;
        parseJsonLy(reply->readAll());
        onPositionChanged(0);
    }
}

void MusicLyric::parseJsonLy(QByteArray jsonByte)
 {
     lrcMap.clear();//清空歌词容器
   QJsonParseError json_error;
   QJsonDocument parse_doucment = QJsonDocument::fromJson(jsonByte,&json_error);
    if(json_error.error == QJsonParseError::NoError)
    {
        if(parse_doucment.isObject())
        {
            QJsonObject rootobj = parse_doucment.object();
            if(rootobj.contains("lrc"))
            {
                QJsonValue valuedata = rootobj.value("lrc");
                if(valuedata.isObject())
                {
                    QJsonObject valuedataObject = valuedata.toObject();
                    if(valuedataObject.contains("lyric"))
                    {
                        QJsonValue play_url_value = valuedataObject.value("lyric");
                        if(play_url_value.isString())
                        {
                            QString play_lrcStr = play_url_value.toString();
                            if(play_lrcStr !="")
                            {
                                QString s = play_lrcStr;
                                QStringList s1 = s.split("\n");
                                for(int i=0;i<s1.size()-1;i++)
                                {
                                    QString ss1 = s1.at(i);
                                    QRegExp ipRegExp = QRegExp("\\[\\d\\d\\S\\d\\d\\S\\d\\d\\]");
                                    bool match = ipRegExp.indexIn(ss1);
                                    if(match == false)
                                    {
                                        int s_1 = ss1.mid(1,2).toInt(); //分
                                        int s_2 = ss1.mid(4,2).toInt(); //秒
                                        int s_3 = ss1.mid(7,2).toInt(); //毫秒
                                        int s_count = (s_1*60+s_2)*100+s_3;
                                        int lrctime = s_count;
                                        QString lrcstr = ss1.mid(10);
                                        //用qmap来保存
                                        lrcMap.insert(lrctime,lrcstr);
                                        qDebug()<<"时间："<<lrctime<<"歌词："<<lrcstr;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {

            }
        }
    }
 }

void MusicLyric::onPositionChanged(qint64 position)
{
    //时间标签得法
    //(分*60+秒)*100+厘秒
    int pos = position/10;
    QMap<int, QString>::iterator iter = lrcMap.begin();
        while (iter != lrcMap.end())
        {
            if(pos-50<=iter.key()&& pos+50>=iter.key())
            {
                    int j=0;
                    if(iter != lrcMap.begin())
                    {
                        iter--;
                        ui->label_20->setText(iter.value());
                        j++;
                    }
                    if(iter != lrcMap.begin())
                    {
                        iter--;
                        ui->label_19->setText(iter.value());
                        j++;
                    }

                    if(iter != lrcMap.begin())
                    {
                        iter--;
                        ui->label_6->setText(iter.value());
                        j++;
                    }
                    for(;j>0;j--)
                    {
                        iter++;
                    }
               //中间
               ui->label_21->setText(iter.value());
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_22->setText(iter.value());
               }
               else
               {
                   ui->label_22->setText("");
                   return;
               }
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_23->setText(iter.value());
               }
               else
               {
                   ui->label_23->setText("");
                   return;
               }
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_24->setText(iter.value());
               }
               else
               {
                   ui->label_24->setText("");
                   return;
               }
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_25->setText(iter.value());
               }
               else
               {
                   ui->label_25->setText("");
                   return;
               }
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_26->setText(iter.value());
               }
               else
               {
                   ui->label_26->setText("");
                   return;
               }
               iter++;
               if(iter != lrcMap.end())
               {
                   ui->label_27->setText(iter.value());
               }
               else
               {
                   ui->label_27->setText("");
                   return;
               }
            }
            iter++;
        }
}


MusicLyric::~MusicLyric()
{
    delete ui;
}
